package com.beagle.njddapp.core;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import com.beagle.njddapp.R;
import com.beagle.njddapp.ui.activity.dirver.DirverOrderActivity;
import com.beagle.njddapp.ui.activity.oil.OilOwnerOrderActivity;
import com.beagle.njddapp.ui.activity.owner.OrderActivity;

import cn.jpush.android.api.CmdMessage;
import cn.jpush.android.api.CustomMessage;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.JPushMessage;
import cn.jpush.android.api.NotificationMessage;
import cn.jpush.android.service.JPushMessageReceiver;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * @author hxx
 * @date 2021-03-29 00:00
 */
public class JPushReeiever extends JPushMessageReceiver {

    private String TAG=JPushReeiever.class.getSimpleName();

    @Override
    public void onMessage(Context context, CustomMessage customMessage) {
        Log.e(TAG,"[onMessage] "+customMessage);
        processCustomMessage(context,customMessage);
    }

    @Override
    public void onNotifyMessageOpened(Context context, NotificationMessage message) {
        Log.e(TAG,"[onNotifyMessageOpened] "+message);

    }

    @Override
    public void onMultiActionClicked(Context context, Intent intent) {
        Log.e(TAG, "[onMultiActionClicked] 用户点击了通知栏按钮");
        String nActionExtra = intent.getExtras().getString(JPushInterface.EXTRA_NOTIFICATION_ACTION_EXTRA);

        //开发者根据不同 Action 携带的 extra 字段来分配不同的动作。
        if(nActionExtra==null){
            Log.d(TAG,"ACTION_NOTIFICATION_CLICK_ACTION nActionExtra is null");
            return;
        }
        if (nActionExtra.equals("my_extra1")) {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮一");
        } else if (nActionExtra.equals("my_extra2")) {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮二");
        } else if (nActionExtra.equals("my_extra3")) {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮三");
        } else {
            Log.e(TAG, "[onMultiActionClicked] 用户点击通知栏按钮未定义");
        }
    }

    @Override
    public void onNotifyMessageArrived(Context context, NotificationMessage message) {
        Log.e(TAG,"[onNotifyMessageArrived] "+message);
    }

    @Override
    public void onNotifyMessageDismiss(Context context, NotificationMessage message) {
        Log.e(TAG,"[onNotifyMessageDismiss] "+message);
    }

    @Override
    public void onRegister(Context context, String registrationId) {
        Log.e(TAG,"[onRegister] "+registrationId);
    }

    @Override
    public void onConnected(Context context, boolean isConnected) {
        Log.e(TAG,"[onConnected] "+isConnected);
    }

    @Override
    public void onCommandResult(Context context, CmdMessage cmdMessage) {
        Log.e(TAG,"[onCommandResult] "+cmdMessage);
    }

    @Override
    public void onTagOperatorResult(Context context,JPushMessage jPushMessage) {
        Log.e(TAG,"[onTagOperatorResult] ");
        super.onTagOperatorResult(context, jPushMessage);
    }
    @Override
    public void onCheckTagOperatorResult(Context context,JPushMessage jPushMessage){
        Log.e(TAG,"[onCheckTagOperatorResult] ");
        super.onCheckTagOperatorResult(context, jPushMessage);
    }
    @Override
    public void onAliasOperatorResult(Context context, JPushMessage jPushMessage) {
        Log.e(TAG,"[onAliasOperatorResult] ");
        super.onAliasOperatorResult(context, jPushMessage);
    }

    @Override
    public void onMobileNumberOperatorResult(Context context, JPushMessage jPushMessage) {
        Log.e(TAG,"[onMobileNumberOperatorResult] ");
        super.onMobileNumberOperatorResult(context, jPushMessage);
    }

    //send msg to MainActivity
    private void processCustomMessage(Context context, CustomMessage customMessage) {
        Log.e(TAG,"[processCustomMessage] ");
        if (customMessage==null){
            return;
        }
        createNotification(context,customMessage.title,customMessage.message);
    }

    private void createNotification(Context context,  String title,String content) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            notifaO(context,title,content);
        }else {
            notifaN(context,title,content);
        }
    }

    private void notifaN(Context context,  String title,String content) {
//        try {
            String channelId = "ChannelId";
            Notification notification = new Notification.Builder(context)
                    .setContentTitle(title)
                    .setContentText(content)
                    .setSmallIcon(R.mipmap.app_jcon)
                    .setContentIntent(getIntent(context))
                    .build();
            NotificationManager manager = (NotificationManager) context
                    .getSystemService(NOTIFICATION_SERVICE);
            manager.notify(36, notification);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
    }
    private void notifaO(Context context, String title,String content) {
            String channelId = "ChannelId";
            Notification notification = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notification = new Notification.Builder(context)
                        .setChannelId(channelId)
                        .setSmallIcon(R.mipmap.app_jcon)
                        .setContentTitle(title)
                        .setContentText(content)
                        .setContentIntent(getIntent(context))
                        .build();
                NotificationManager notificationManager = (NotificationManager) context
                        .getSystemService(NOTIFICATION_SERVICE);
                NotificationChannel channel = new NotificationChannel(
                        channelId,
                        "notify",
                        NotificationManager.IMPORTANCE_DEFAULT);
                notificationManager.createNotificationChannel(channel);
                notificationManager.notify(36, notification);
            }


    }

    private PendingIntent getIntent(Context context  ) {
//        try {
        if (UserService.getInstance()!=null){
            if (UserService.getInstance().getRole()==UserService.ROLE_OWNER){
                Intent intent =new Intent(context, OrderActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                return PendingIntent.getActivity(
                        context,
                        (int) System.currentTimeMillis(),
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );
            } else if (UserService.getInstance().getRole()==UserService.ROLE_OIL){
                Intent intent =new Intent(context, OilOwnerOrderActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                return PendingIntent.getActivity(
                        context,
                        (int) System.currentTimeMillis(),
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );
            }else if (UserService.getInstance().getRole()==UserService.ROLE_DIRVER){
                Intent intent =new Intent(context, DirverOrderActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                return PendingIntent.getActivity(
                        context,
                        (int) System.currentTimeMillis(),
                        intent,
                        PendingIntent.FLAG_CANCEL_CURRENT
                );
            }
        }

        return  null;

    }

    @Override
    public void onNotificationSettingsCheck(Context context, boolean isOn, int source) {
        super.onNotificationSettingsCheck(context, isOn, source);
        Log.e(TAG,"[onNotificationSettingsCheck] isOn:"+isOn+",source:"+source);
    }}

